package com.yuuzy.view;

import com.yuuzy.dao.WipDAO;
import com.yuuzy.model.Wip;
import com.yuuzy.utils.StringUtils;

import javax.swing.*;
import javax.swing.GroupLayout.Alignment;
import javax.swing.LayoutStyle.ComponentPlacement;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class InsertWip extends JInternalFrame {
    private final ButtonGroup group = new ButtonGroup();
    private JTextField wip_nameText;
    private JTextField wip_amountText;
    private JTextField wip_yearText;
    private JTextArea wip_descriptionText;
    private JRadioButton wip_upRB;
    private JRadioButton wip_downRB;
    private WipDAO wipDAO = new WipDAO();

    /**
     * Create the frame.
     */
    public InsertWip() {
        setClosable(true);
        setIconifiable(true);
        setTitle("添加理财产品");
        setBounds(100, 100, 763, 736);

        JLabel lblNewLabel = new JLabel("理财产品名称：");

        JLabel label = new JLabel("理财产品介绍：");

        JLabel label_1 = new JLabel("理财产品价格：");

        JLabel label_2 = new JLabel("理财产品年限：");

        JLabel label_3 = new JLabel("是否上线：");

        wip_nameText = new JTextField();
        wip_nameText.setColumns(10);

        wip_descriptionText = new JTextArea();

        wip_amountText = new JTextField();
        wip_amountText.setColumns(10);

        wip_yearText = new JTextField();
        wip_yearText.setColumns(10);

        wip_upRB = new JRadioButton("是");
        group.add(wip_upRB);
        wip_upRB.setSelected(true);

        wip_downRB = new JRadioButton("否");
        group.add(wip_downRB);

        JButton button = new JButton(" 添加 ");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                insertActionPerformed(e);
            }
        });
        button.setIcon(new ImageIcon(InsertWip.class.getResource("/image/addition.png")));

        JButton button_1 = new JButton(" 重置 ");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                resetValueActionPerformed(e);
            }
        });
        button_1.setIcon(new ImageIcon(InsertWip.class.getResource("/image/reset.png")));
        GroupLayout groupLayout = new GroupLayout(getContentPane());
        groupLayout.setHorizontalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(196)
                                                .addComponent(button, GroupLayout.PREFERRED_SIZE, 108, GroupLayout.PREFERRED_SIZE)
                                                .addGap(115)
                                                .addComponent(button_1, GroupLayout.PREFERRED_SIZE, 116, GroupLayout.PREFERRED_SIZE))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addGap(134)
                                                                .addComponent(label, GroupLayout.PREFERRED_SIZE, 144, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addGap(136)
                                                                .addComponent(lblNewLabel))
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addGap(130)
                                                                .addComponent(label_1, GroupLayout.PREFERRED_SIZE, 155, GroupLayout.PREFERRED_SIZE)))
                                                .addGap(37)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addComponent(wip_amountText, GroupLayout.PREFERRED_SIZE, 251, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(wip_nameText, GroupLayout.PREFERRED_SIZE, 251, GroupLayout.PREFERRED_SIZE)
                                                        .addComponent(wip_descriptionText, GroupLayout.PREFERRED_SIZE, 249, GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addGap(128)
                                                                .addComponent(label_3, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE))
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addGap(129)
                                                                .addComponent(label_2, GroupLayout.PREFERRED_SIZE, 152, GroupLayout.PREFERRED_SIZE)))
                                                .addGap(40)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                                        .addGroup(groupLayout.createSequentialGroup()
                                                                .addComponent(wip_upRB)
                                                                .addGap(87)
                                                                .addComponent(wip_downRB, GroupLayout.PREFERRED_SIZE, 51, GroupLayout.PREFERRED_SIZE))
                                                        .addComponent(wip_yearText, GroupLayout.PREFERRED_SIZE, 251, GroupLayout.PREFERRED_SIZE))))
                                .addContainerGap(174, Short.MAX_VALUE))
        );
        groupLayout.setVerticalGroup(
                groupLayout.createParallelGroup(Alignment.LEADING)
                        .addGroup(groupLayout.createSequentialGroup()
                                .addGap(84)
                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                        .addComponent(lblNewLabel)
                                        .addComponent(wip_nameText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(55)
                                                .addComponent(label)
                                                .addGap(148)
                                                .addGroup(groupLayout.createParallelGroup(Alignment.BASELINE)
                                                        .addComponent(label_1)
                                                        .addComponent(wip_amountText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE)))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(52)
                                                .addComponent(wip_descriptionText, GroupLayout.PREFERRED_SIZE, 120, GroupLayout.PREFERRED_SIZE)))
                                .addPreferredGap(ComponentPlacement.RELATED, 56, Short.MAX_VALUE)
                                .addGroup(groupLayout.createParallelGroup(Alignment.TRAILING)
                                        .addComponent(label_2)
                                        .addComponent(wip_yearText, GroupLayout.PREFERRED_SIZE, GroupLayout.DEFAULT_SIZE, GroupLayout.PREFERRED_SIZE))
                                .addPreferredGap(ComponentPlacement.RELATED)
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(50)
                                                .addComponent(wip_upRB))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(49)
                                                .addComponent(wip_downRB))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(58)
                                                .addComponent(label_3)))
                                .addGroup(groupLayout.createParallelGroup(Alignment.LEADING)
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(54)
                                                .addComponent(button_1))
                                        .addGroup(groupLayout.createSequentialGroup()
                                                .addGap(56)
                                                .addComponent(button)))
                                .addContainerGap(86, Short.MAX_VALUE))
        );
        getContentPane().setLayout(groupLayout);

    }

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    InsertWip frame = new InsertWip();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * 添加事件处理
     *
     * @param evt
     */
    private void insertActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        String wip_name = this.wip_nameText.getText();
        String wip_description = this.wip_descriptionText.getText();
        String wip_amount = this.wip_amountText.getText();
        String wip_year = this.wip_yearText.getText();

        if (StringUtils.isEmpty(wip_name)) {
            JOptionPane.showMessageDialog(null, "理财产品名称不能为空！");
            return;
        }

        if (StringUtils.isEmpty(wip_description)) {
            JOptionPane.showMessageDialog(null, "理财产品介绍不能为空！");
            return;
        }

        if (StringUtils.isEmpty(wip_amount)) {
            JOptionPane.showMessageDialog(null, "理财产品价格不能为空！");
            return;
        }

        if (StringUtils.isEmpty(wip_year)) {
            JOptionPane.showMessageDialog(null, "理财产品年限不能为空！");
            return;
        }

        String wip_up = "";
        if (wip_upRB.isSelected()) {
            wip_up = "1";
        } else if (wip_downRB.isSelected()) {
            wip_up = "0";
        }

        if (StringUtils.isEmpty(wip_up)) {
            JOptionPane.showMessageDialog(null, "请选择是否上线该理财产品！");
            return;
        }

        Wip wip = new Wip(wip_name, wip_description, StringUtils.changeDouble(wip_amount), StringUtils.changeInt(wip_year), wip_up);
        int insertNum = wipDAO.insertWip(wip);
        if (insertNum > 0) {
            JOptionPane.showMessageDialog(null, "理财产品添加成功！");
            resetValue();
        } else {
            JOptionPane.showMessageDialog(null, "理财产品添加失败！");
        }
    }


    /**
     * 重置事件处理
     *
     * @param evt
     */
    private void resetValueActionPerformed(ActionEvent evt) {
        // TODO Auto-generated method stub
        this.resetValue();
    }

    //重置表单
    private void resetValue() {
        // TODO Auto-generated method stub
        this.wip_nameText.setText("");
        this.wip_descriptionText.setText("");
        this.wip_amountText.setText("");
        this.wip_yearText.setText("");
        this.group.clearSelection();
    }

}
